package com.baidu.customview.widget.progressbar;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.baidu.customview.R;


/**
 * Created by imagine on 2015/9/29.
 */
public class BaseProgress extends View {
    private int circleColor;
    private float circleStrokeWidth;
    private int textColor;
    private int mProgress;
    private int mTextProgress;

    public int getmTextProgress() {
        return mTextProgress;
    }

    public synchronized void setmTextProgress(int mTextProgress) {
        this.mTextProgress = mTextProgress;
        postInvalidate();
    }

    public int getCircleColor() {
        return circleColor;
    }

    public void setCircleColor(int circleColor) {
        this.circleColor = circleColor;
    }

    public float getCircleStrokeWidth() {
        return circleStrokeWidth;
    }

    public void setCircleStrokeWidth(float circleStrokeWidth) {
        this.circleStrokeWidth = circleStrokeWidth;
    }

    public int getmProgress() {
        return mProgress;
    }

    public synchronized void setmProgress(int mProgress) {
        this.mProgress = mProgress;
//        local area refresh
        postInvalidate();
    }

    public Paint getPaint() {
        return paint;
    }

    public void setPaint(Paint paint) {
        this.paint = paint;
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    Paint paint = new Paint();

    public BaseProgress(Context context) {
        super(context);
        Log.e("A---", "------");
    }

    public BaseProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.e("B---", "------");
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray mTypeArray = context.obtainStyledAttributes(attrs, R.styleable.BaseProgress);
        circleColor = mTypeArray.getColor(R.styleable.BaseProgress_circleColor, Color.RED);
        circleStrokeWidth = mTypeArray.getDimension(R.styleable.BaseProgress_circleStrokeWidth, 50);
        textColor = mTypeArray.getColor(R.styleable.BaseProgress_textColor, Color.GREEN);
        mProgress = mTypeArray.getInteger(R.styleable.BaseProgress_mProgress, 5);
        mProgress = mTypeArray.getInteger(R.styleable.BaseProgress_mProgress, 1);
        mTypeArray.recycle();
    }

    public BaseProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.e("C---", "-----");
//        TypedArray mTypeArray=context.obtainStyledAttributes(attrs, R.styleable.BaseProgress);
//        circleColor=mTypeArray.getColor(R.styleable.BaseProgress_circleColor,Color.RED);
//        circleStrokeWidth=mTypeArray.getDimension(R.styleable.BaseProgress_circleStrokeWidth, 50);
//        textColor=mTypeArray.getColor(R.styleable.BaseProgress_textColor, Color.GREEN);
//
//
//        mTypeArray.recycle();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BaseProgress(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(circleColor);
        paint.setStrokeWidth(circleStrokeWidth);
        paint.setColor(Color.GRAY);
        canvas.drawCircle(400, 400, 200, paint);

//        set progress
        paint.setColor(Color.BLUE);
        paint.setAntiAlias(true);
        RectF r1 = new RectF(200, 200, 600, 600);
        canvas.drawArc(r1, 0, (float) (3.6 * mProgress), false, paint);

//        set textProgress
        paint.setColor(textColor);
        paint.setTextSize(50);
        paint.setStrokeWidth(1);
        canvas.drawText(mTextProgress + "%", 380, 400, paint);

    }
}
